package chapter05

object Test06_HiggOrderFunction {

  def main(args: Array[String]): Unit = {
    def f(n: Int): Int = {
      println("n=" + n)
      n + 1
    }

    val result: Int = f(123)
    println(result)
    //1 函数作为值进行传递
    val f1: Int => Int = f
    val f2 = f _ //将f函数传给f2
    println(f1(456))
    println(f2)

    def fun(): Int = 123

    val f3 = fun _; //表示指向这个函数体
    val f4: () => Int = fun _
    println(fun _)
    println(f3)

    //2.函数作为函数的参数进行传递
    def dualEval(op: (Int, Int) => Int, a: Int, b: Int): Int = op(a, b)

    def add(a: Int, b: Int): Int = a + b

    val i = dualEval(add, 23, 56)
    val i1 = dualEval(
      (a, b) => a * b,
      32, 65
    )
    println("i=" + i)
    println("i1=" + i1)

    //3.函数作为函数的返回值进行返回
    def f5(): Int => Unit = {
      def f6(a: Int): Unit = println("f6调用: " + a)

      f6 //当函数直接返回
    }

    val f6 = f5();
    println(f6(12))
  }
}